package com.zoe.thread.th12_线程池自定义;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.thread.ThreadUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.*;

/**
 * @author Zoe.Zhang
 * @date 2021/11/30
 * @description
 */
public class MyThreadList {
    private static final Logger logger = LoggerFactory.getLogger(MyThreadList.class);
    public static void main(String[] args) throws InterruptedException {
        List<String> listAlls    = CollUtil.newArrayList();
        listAlls.add("1");
        listAlls.add("2");
        listAlls.add("3");
        listAlls.add("4");
        listAlls.add("5");
        listAlls.add("6");
        listAlls.add("7");
        listAlls.add("8");
        listAlls.add("9");

        ExecutorService executorService = new ThreadPoolExecutor(
                2,
                2,
                1,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(3),
                Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

        myExecute(executorService,listAlls);

        executorService.shutdown();
        System.out.println("endddddddddddddd");
    }
    public static void myExecute(ExecutorService executorService,List<String> list) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(list.size());
        for (String li : list) {
            try {
                executorService.execute(()->{
                    System.out.println(li);
                    ThreadUtil.sleep(1000);
                });
            }
            catch (Exception e){
               logger.error("任务丢失====>{}",li);
            }
            finally {
             countDownLatch.countDown();
             System.out.println("count==="+countDownLatch.getCount());
            }
        }

    }
}
